package com.exrobot.module.rbt.service.canteeninfo;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.exrobot.module.rbt.controller.admin.canteeninfo.vo.*;
import com.exrobot.module.rbt.dal.dataobject.canteeninfo.CanteenInfoDO;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.util.object.BeanUtils;

import com.exrobot.module.rbt.dal.mysql.canteeninfo.CanteenInfoMapper;

import static com.exrobot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.exrobot.framework.common.util.collection.CollectionUtils.convertList;
import static com.exrobot.framework.common.util.collection.CollectionUtils.diffList;
import static com.exrobot.module.rbt.enums.ErrorCodeConstants.*;

/**
 * 食堂信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class CanteenInfoServiceImpl implements CanteenInfoService {

    @Resource
    private CanteenInfoMapper canteenInfoMapper;

    @Override
    public Long createCanteenInfo(CanteenInfoSaveReqVO createReqVO) {
        // 插入
        CanteenInfoDO canteenInfo = BeanUtils.toBean(createReqVO, CanteenInfoDO.class);
        canteenInfoMapper.insert(canteenInfo);

        // 返回
        return canteenInfo.getId();
    }

    @Override
    public void updateCanteenInfo(CanteenInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateCanteenInfoExists(updateReqVO.getId());
        // 更新
        CanteenInfoDO updateObj = BeanUtils.toBean(updateReqVO, CanteenInfoDO.class);
        canteenInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteCanteenInfo(Long id) {
        // 校验存在
        validateCanteenInfoExists(id);
        // 删除
        canteenInfoMapper.deleteById(id);
    }

    @Override
    public void deleteCanteenInfoListByIds(List<Long> ids) {
        // 删除
        canteenInfoMapper.deleteByIds(ids);
    }


    private void validateCanteenInfoExists(Long id) {
        if (canteenInfoMapper.selectById(id) == null) {
            throw exception(RBT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public CanteenInfoDO getCanteenInfo(Long id) {
        return canteenInfoMapper.selectById(id);
    }

    @Override
    public CanteenInfoDO getCanteenInfoBySn(Long sn) {
        return canteenInfoMapper.selectFirstOne(CanteenInfoDO::getCanteenSn,sn);
    }

    @Override
    public PageResult<CanteenInfoDO> getCanteenInfoPage(CanteenInfoPageReqVO pageReqVO) {
        return canteenInfoMapper.selectPage(pageReqVO);
    }

}